home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / daemons / ipServer / tcpTrace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-26  |  5.6 KB  |  242 lines

  1. /*
  2.  * tcpTrace.c --
  3.  *
  4.  *    Routines to log traces of major events that occur for a TCP socket.
  5.  *
  6.  *    Based on 4.3BSD    @(#)tcp_debug.c    7.1 (Berkeley) 6/5/86
  7.  *
  8.  * Copyright 1987 Regents of the University of California
  9.  * All rights reserved.
  10.  * Permission to use, copy, modify, and distribute this
  11.  * software and its documentation for any purpose and without
  12.  * fee is hereby granted, provided that the above copyright
  13.  * notice appear in all copies.  The University of California
  14.  * makes no representations about the suitability of this
  15.  * software for any purpose.  It is provided "as is" without
  16.  * express or implied warranty.
  17.  */
  18.  
  19. #ifndef lint
  20. static char rcsid[] = "$Header: /sprite/src/daemons/ipServer/RCS/tcpTrace.c,v 1.3 89/03/23 09:57:03 brent Exp $ SPRITE (Berkeley)";
  21. #endif not lint
  22.  
  23.  
  24. #include "sprite.h"
  25. #include "netInet.h"
  26. #include "ipServer.h"
  27. #include "tcp.h"
  28. #include "tcpInt.h"
  29. #include "tcpTimer.h"
  30.  
  31. /*
  32.  * A TraceRecord contains a snapshot of important information.
  33.  */
  34. typedef  struct    {
  35.     int            time;        /* Time when the trace routine was 
  36.                      * called. */
  37.     TCPTraceCmd        command;    /* The type of tracing desired. */
  38.     int            prevState;    /* Previous state of the TCP control 
  39.                      * block. */
  40.     Net_TCPHeader    header;        /* TCP header to be logged. */
  41.     TCPControlBlock    controlBlock;    /* Contents of the control block. */
  42. } TraceRecord;
  43.  
  44. static char    *traceNames[] = {
  45.     "Input", "Output", "Respond", "Drop"
  46. };
  47.  
  48. static char *flagNames[] = { "FIN", "SYN", "RESET", "PUSH", "ACK", "URG",};
  49.  
  50. char *tcbStateNames[] = {
  51.     "CLOSED",
  52.     "LISTEN",
  53.     "SYN_SENT",
  54.     "SYN_RECEIVED",
  55.     "ESTABLISHED",
  56.     "CLOSE_WAIT",
  57.     "LAST_ACK",
  58.     "FIN_WAIT_1",
  59.     "FIN_WAIT_2",
  60.     "CLOSING",
  61.     "TIME_WAIT",
  62. };
  63.  
  64. #define NUM_TRACES 100
  65. static TraceRecord traceArray[NUM_TRACES];
  66. static int traceNum = 0;
  67.  
  68.  
  69.  
  70. /*
  71.  *----------------------------------------------------------------------
  72.  *
  73.  * TCPTrace --
  74.  *
  75.  *    Used to trace important changes to a TCP control block.
  76.  *
  77.  * Results:
  78.  *    None.
  79.  *
  80.  * Side effects:
  81.  *    The traceArray is updated.
  82.  *
  83.  *----------------------------------------------------------------------
  84.  */
  85.  
  86. void
  87. TCPTrace(command, prevState, tcbPtr, headerPtr, dataLen)
  88.     TCPTraceCmd        command;    /* Type of trace. */
  89.     int            prevState;    /* Previous TCB state. */
  90.     TCPControlBlock    *tcbPtr;    /* TCB to be saved. */
  91.     Net_TCPHeader    *headerPtr;    /* TCP header to be saved. */
  92.     int            dataLen;    /* Amount of data in the packet. */
  93. {
  94.     register TraceRecord *tracePtr;
  95.     TCPSeqNum    seq;
  96.     TCPSeqNum    ack;
  97.  
  98.     tracePtr = &traceArray[traceNum];
  99.     traceNum += 1;
  100.     if (traceNum == NUM_TRACES) {
  101.     traceNum = 0;
  102.     }
  103.  
  104.     tracePtr->time    = IPS_GetTimestamp();
  105.     tracePtr->command    = command;
  106.     tracePtr->prevState    = prevState;
  107.  
  108.     if (tcbPtr != NULL) {
  109.     tracePtr->controlBlock = *tcbPtr;
  110.     } else {
  111.     bzero((Address)&tracePtr->controlBlock, sizeof(*tcbPtr));
  112.     }
  113.     if (headerPtr != NULL) {
  114.     tracePtr->header = *headerPtr;
  115.     } else {
  116.     bzero((Address)&tracePtr->header, sizeof(*headerPtr));
  117.     }
  118.  
  119.     if (!ips_Debug) {
  120.     return;
  121.     }
  122.  
  123.     (void) printf("%s: ", traceNames[(int)command]);
  124.  
  125.     if (tcbPtr != NULL) {
  126.     (void) printf("%x %s:", tcbPtr, tcbStateNames[(int)prevState]);
  127.     } else {
  128.     (void) printf("???????? ");
  129.     }
  130.  
  131.     switch (command) {
  132.  
  133.     case TCP_TRACE_INPUT:
  134.     case TCP_TRACE_OUTPUT:
  135.     case TCP_TRACE_DROP:
  136.         if (headerPtr == NULL) {
  137.         break;
  138.         }
  139.         seq = headerPtr->seqNum;
  140.         ack = headerPtr->ackNum;
  141.         if (command == TCP_TRACE_OUTPUT) {
  142.         seq = Net_NetToHostInt(seq);
  143.         ack = Net_NetToHostInt(ack);
  144.         }
  145.         if (dataLen != 0) {
  146.         (void) printf("[%x..%x)", seq, seq+dataLen);
  147.         } else {
  148.         (void) printf("%x", seq);
  149.         }
  150.         (void) printf("@%x, urgent=%x", ack, headerPtr->urgentOffset);
  151.         if (headerPtr->flags!= 0) {
  152.         TCPPrintHdrFlags(stdout, headerPtr->flags);
  153.         }
  154.         break;
  155.     }
  156.  
  157.     /* 
  158.      * Print out internal state of *tcbPtr.
  159.      */
  160.  
  161.     if (tcbPtr != NULL) {
  162.     (void) printf(" -> %s\n", tcbStateNames[(int)tcbPtr->state]);
  163.  
  164.     (void) printf("\trecv.(next,window,urgPtr) (%x,%x,%x)\n",
  165.         tcbPtr->recv.next, tcbPtr->recv.window, tcbPtr->recv.urgentPtr);
  166.     (void) printf("\tsend.(unAck,next,maxSent) (%x,%x,%x)\n",
  167.         tcbPtr->send.unAck, tcbPtr->send.next, tcbPtr->send.maxSent);
  168.     (void) printf("\tsend.(updateSeq#,updateAck#,window) (%x,%x,%x)\n",
  169.         tcbPtr->send.updateSeqNum, tcbPtr->send.updateAckNum, 
  170.         tcbPtr->send.window);
  171.     }
  172.     (void) printf("\n");
  173. }
  174.  
  175.  
  176. /*
  177.  *----------------------------------------------------------------------
  178.  *
  179.  * TCPPrintHdrFlags --
  180.  *
  181.  *    Prints the state of the flags in the TCP header.
  182.  *
  183.  * Results:
  184.  *    None.
  185.  *
  186.  * Side effects:
  187.  *    None.
  188.  *
  189.  *----------------------------------------------------------------------
  190.  */
  191.  
  192. void
  193. TCPPrintHdrFlags(stream, flags)
  194.     FILE        *stream;    /* Where to print. */
  195.     unsigned short    flags;        /* TCP packet header flags. */
  196. {
  197.     register int i;
  198.  
  199.     char *cp = "<";
  200.     for (i=0; i < 6; i++) {
  201.     if ((1 << i) & flags) {
  202.         (void) fprintf(stream, "%s%s", cp, flagNames[i]);
  203.         cp = ", ";
  204.     }
  205.     }
  206.     (void) fprintf(stream, ">");
  207. }
  208.  
  209.  
  210. /*
  211.  *----------------------------------------------------------------------
  212.  *
  213.  * TCP_PrintInfo --
  214.  *
  215.  *    A routine to print out the state of a TCB.
  216.  *
  217.  * Results:
  218.  *    None.
  219.  *
  220.  * Side effects:
  221.  *    None.
  222.  *
  223.  *----------------------------------------------------------------------
  224.  */
  225.  
  226. void
  227. TCP_PrintInfo(data)
  228.     ClientData    data;        /* Really a pointer to a TCP. */
  229. {
  230.     TCPControlBlock *tcbPtr = (TCPControlBlock *) data;
  231.  
  232.     if (tcbPtr != (TCPControlBlock *) NULL) {
  233.     (void) fprintf(stderr, 
  234.         "%10s flags=%x unAck=%d s.next=%d r.next=%d\n",
  235.         tcbStateNames[(int)tcbPtr->state],
  236.         tcbPtr->flags,
  237.         tcbPtr->send.unAck,
  238.         tcbPtr->send.next,
  239.         tcbPtr->recv.next);
  240.     }
  241. }
  242.